package com.hll.javaweb.servlet.dispatcher;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author hll
 * @create 2022-05-10 12:32
 */
@WebServlet("/hello-dis-01")
public class HelloDispatcher_01 extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.getRequestDispatcher("/pages/hello.html").forward(req, resp);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("*************这里是dis-01");
    // 当然前台参数也是可以获取的
    req.setAttribute("dis1", "我已经看过了"); // 可以设置request属性
    // 注意 / 开头默认拼接web项目资源路径，与html中不太一样（一个是服务器解析，一个是浏览器）
    RequestDispatcher requestDispatcher = req.getRequestDispatcher("/hello-dis-02");
    // RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com");

    // 请求转发，dis-01可以做一些预处理，实际处理教育dis-02
    requestDispatcher.forward(req, resp);
  }
}
